<template>
  <div>
    <lb-title></lb-title>
    <div class="page-main search-form">
      <el-row class="page-top-operate">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="$router.push('/editUpload')"
          >{{ $t('menu.AddUpload') }}</el-button
        >
      </el-row>
      <div class="page-top-operate form-search">
        <el-form @submit.native.prevent :inline="true" :model="pageForm">
          <el-form-item label="配置名称">
            <el-input
              v-model="pageForm.name"
              placeholder="请输入配置名称"
            ></el-input>
          </el-form-item>
          <lb-button type="primary" @click="getTableDataList(1)">{{
            $t('action.search')
          }}</lb-button>
        </el-form>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="100px"> </el-table-column>
        <el-table-column prop="name" label="配置名称"> </el-table-column>
        <el-table-column prop="open_oss" label="上传类型">
          <template slot-scope="scope">
            <div>
              {{
                scope.row.open_oss === 0
                  ? '本地上传'
                  : scope.row.open_oss === 1
                  ? '阿里云OSS上传'
                  : scope.row.open_oss === 2
                  ? '七牛云上传'
                  : '腾讯云上传'
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <el-button
                type="primary"
                size="mini"
                plain
                @click="$router.push('/editUpload?id=' + scope.row.id)"
                >{{ $t('action.edit') }}</el-button
              >
              <el-button
                type="danger"
                size="mini"
                plain
                @click="deleteAuthInfo(scope.row.id)"
                >{{ $t('action.delete') }}</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :page="pageForm.page"
        :pageSize="pageForm.page_count"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  name: 'auth',
  data () {
    return {
      loading: false,
      pageForm: {
        page: 1,
        page_count: 10,
        name: ''
      },
      total: 0,
      tableData: []
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList (flag) {
      this.loading = true
      if (flag) this.pageForm.page = 1
      let { pageForm } = this
      this.$api.uploadConfigList(pageForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    deleteAuthInfo (id) {
      this.$confirm(this.$t('tips.confirmDelete') + '删除后将无法恢复！', this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.$api.uploadConfigDel({ id }).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successDel'))
            this.pageForm.page = this.pageForm.page < Math.ceil((this.total - 1) / this.pageForm.page_count) ? this.pageForm.page : Math.ceil((this.total - 1) / this.pageForm.page_count)
            this.getTableDataList()
          }
        })
      })
    },
    handleSizeChange (val) {
      this.pageForm.page_count = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.pageForm.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
